package com.person.service.impl;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.person.common.BaseContext;
import com.person.common.CustomException;
import com.person.mapper.CategoryMapper;
import com.person.mapper.DishMapper;
import com.person.mapper.SetmealMapper;
import com.person.pojo.Category;
import com.person.pojo.PageBean;
import com.person.service.CategoryService;
import com.person.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void addCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());

        log.info("添加菜品分类{}",category);
        categoryMapper.addCategory(category);
    }

    //分页查询
    @Override
    public PageBean page(Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);

        //使用sort方法匿名内部类的方式排序
        List<Category> categories = categoryMapper.inquire();
        Collections.sort(categories,new Comparator<Category>() {
            @Override
            public int compare(Category o1, Category o2) {
                return o1.getSort() - o2.getSort();
            }});

        Page<Category> p =(Page<Category>) categories;
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }



    //删除分类之前需要判断
    @Override
    public void deleteCateGory(Long id) {
        //查询当前分类是否关连了菜品
        int countDish = dishMapper.selectDish(id);
        log.info("关联菜品个数：{}",countDish);
        //如果分类下有菜品，则抛出异常
        if (countDish>0){
            throw new CustomException("当前分类下关联了菜品,不能删除");
        }

        //查询当前分类下是否关联了套餐
        int countSetmeal= setmealMapper.selectSetmeal(id);
        log.info("关联套餐个数：{}",countSetmeal);
        //如果分类下有套餐，抛出异常
        if (countSetmeal>0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        //如果正常直接删除
        categoryMapper.delete(id);
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public List<Category> categoryList(Category category) {
        List<Category> categories = categoryMapper.selectCategory(category);
        Collections.sort(categories, new Comparator<Category>() {
            @Override
            public int compare(Category o1, Category o2) {
                return o1.getSort()-o2.getSort();
            }
        });
        return categories;
    }


}
